#!/usr/bin/env python

import autorm.model
import autorm.db.query

class Model(autorm.model.Model):
    '''Model class
    
    @author Julot <andy.julot@gmail.com>
    @since Wednesday, January 25, 2012. 06:06 PM'''
    
    @classmethod
    def createTable(cls):
        '''Create table'''
        
        cls.objects.create_table_if_needed()
    
    @classmethod
    def dropTable(cls):
        '''Drop table'''
        
        cmd = 'DROP TABLE IF EXISTS [%s];'
        autorm.db.query.Query.raw_sql(cmd % cls.Meta.table)
    
    @classmethod
    def createIndex(cls):
        '''Create table index as determine in Meta'''
        
        if hasattr(cls.Meta, 'fields'):
            cmd = 'CREATE INDEX IF NOT EXISTS [index_%s_%s] ON [%s] ([%s] ASC);'
            for f in cls.Meta.fields:
                if f.index:
                    data = (cls.Meta.table, f.name, cls.Meta.table, f.name)
                    autorm.db.query.Query.raw_sql(cmd % data)
    
    @classmethod
    def dropIndex(cls):
        '''Drop index'''
        
        if hasattr(cls.Meta, 'fields'):
            cmd = 'DROP INDEX IF EXISTS [index_%s_%s];'
            for f in cls.Meta.fields:
                if f.index:
                    data = (cls.Meta.table, f.name)
                    autorm.db.query.Query.raw_sql(cmd % data)
    
    @classmethod
    def vacuum(cls):
        autorm.db.query.Query.raw_sql('VACUUM;')